package com.troila.rpc;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value = 2)
@Slf4j
public class RpcServerLauncher extends Thread implements ApplicationRunner {

    private static final String THREAD_NAME = "rpc-server";

    private RpcServer rpcServer;

    @Autowired
    public void setRpcServer(RpcServer rpcServer) {
        this.rpcServer = rpcServer;
    }

    @Override
    public void run(ApplicationArguments args) {
        this.setName(THREAD_NAME);
        this.start();
    }

    @Override
    public void run() {
        try {
            rpcServer.start();
        } catch (Exception e) {
            log.error("error launching RPCServer", e);
            throw new RuntimeException(e);
        }
    }
}
